#!/bin/bash
# 本地交互版，用于单台在机器在本地执行脚本

salt_config_file=/etc/salt/minion
salt_script=/etc/init.d/salt-minion
zabbix_config_file=/usr/local/zabbix/etc/zabbix_agentd.conf
zabbix_script=/etc/init.d/zabbix_agentd
# ip_list_file=/data/tx_move.ini
# import_script=/data/script/ImportRes.py


colorPrint(){
	case $1 in
	g)
		echo -e "\033[32m$2\033[0m"
		;;
	r)
		echo -e "\033[31m$2\033[0m"
		;;
	b)
		echo -e "\033[34m$2\033[0m"
		;;
	*)
		echo "usage:`basename $0` 颜色 显示内容"
		;;
	esac
}

changeSaltConfig(){
	# 停止 salt-minion
	ps aux | grep salt-minion | grep -v grep &> /dev/null
	[ $? -ne 0 ] && colorPrint 'g' 'salt-minion 正在运行中，需要先停止' && read -p "请问是否停止salt-minion(y/n):" stop_choice
	[ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ] && \ 
	for i in `ps aux | grep salt-minion | grep -v grep | awk '{print $2}'`;do kill -9 $i ;done || \
	colorPrint 'r' '不停止salt，退出程序' && exit 1 

	# 修改配置文件
	local_ip=`ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'`
	colorPrint 'g' "当前机器内网为：$local_ip,开始修改salt配置文件"
	sed -ri 's/(game|admin|pay|login)-.*/\1-$local_ip/g' $salt_config_file
	colorPrint 'g' "修改后的配置文件内容为 \n`tail -2 $salt_config_file`"
	colorPrint 'g' '开始启动salt-minion================='
	$salt_script start
	colorPrint 'g' 'salt-minion 启动进程如下：'
	ps aux | grep salt-minion | grep -v grep
}

changZabbixConfig(){
	# 停止 zabbix_agent
	ps aux | grep zabbix_agentd | grep -v grep &> /dev/null
	[ $? -ne 0 ] && colorPrint 'g' 'zabbix_agentdb 正在运行中，需要先停止' && read -p "请问是否停止zabbix_agentd(y/n):" stop_choice
	[ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ] && $zabbix_script stop || colorPrint 'r' '不停止zabbix_agentd，退出程序' && exit 1
	# [ -nz $stop_choice -a $choice == 'y' -o $choice == 'Y' ] && \ 
	# for i in `ps aux | grep zabbix_agentd | grep -v grep | awk '{print $2}'`;do kill -9 $i ;done || \
	# colorPrint 'r' '不停止zabbix_agentd，退出程序' && exit 1 

	# 修改配置文件
	local_ip=`ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'`
	colorPrint 'g' "当前机器内网为：$local_ip,开始修改zabbix_agentd配置文件"
	sed -ri 's/(Hostname=).*/\1$local_ip/g' $zabbix_config_file
	colorPrint 'b' "修改后的配置文件内容为 \n`tail -2 $zabbix_config_file`"
	colorPrint 'g' '开始启动zabbix_agentd================='
	$zabbix_script start
	colorPrint 'g' 'zabbix_agentd 启动进程如下：'
	ps aux | grep zabbix_agentd | grep -v grep	
}

# changeResource
#changeSaltConfig
getRemoteInnerIP(){
	ifconfig | grep -A 1 'eth0' | grep inet | awk '{print $2}' | awk -F: '{print $2}'
}

stopMysql(){
	/etc/init.d/mysqld stop
}

# changeResource(){
# 	ip_list=''
# 	for i in `awk '{print $1}' $ip_list_file`;do
# 		local_ip=`getRemoteInnerIP "$i"`
# 		ip_list=${ip_list},${local_ip}:
# 	done
# 	ip_list=`echo $ip_list | sed -nr 's/(.*):$/\1/gp'`
# 	# python $import_script -f $ip_list
# 	echo $ip_list
# }

case $1 in
'changeSaltConfig')
	changeSaltConfig
	;;
'changZabbixConfig')
	changZabbixConfig
	;;
'getRemoteInnerIP')
	getRemoteInnerIP
	;;
'stopMysql')
	stopMysql
	;;
*)
	colorPrint 'g' '`usage:basename $0` 函数名称'
	;;
esac